package controllers

import (
	"cvevulner/errcode"
	"cvevulner/models"
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
)

type IssueOathCallbackController struct {
	beego.Controller
}

func (c *IssueOathCallbackController) RetData(resp map[string]interface{}) {
	c.Data["json"] = resp
	c.ServeJSON()
}

type IssueRepoWhiteData struct {
	RepoId      int64  `json:"id"`
	PackageName string `json:"packageName"`
	Version     string `json:"version"`
	Branchs     string `json:"branchs"`
	Status      int8   `json:"status"`
	CreateTime  string `json:"createTime"`
	UpdateTime  string `json:"updateTime"`
	DeleteTime  string `json:"deleteTime"`
}


// @Title UserLogin
// @Description UserLogin
// @Param	body		body 	models.User	true		"body for user content"
// @Success 200 {int} models.User.ID
// @Failure 403 body is empty
// @router / [post]
func (c *IssueOathCallbackController) Post() {
	req := make(map[string]interface{})
	resp := make(map[string]interface{})
	resp["errno"] = errcode.RecodeLoginErr
	resp["errmsg"] = errcode.RecodeText(errcode.RecodeLoginErr)
	resp["body"] = Result{}
	defer c.RetData(resp)
	json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	logs.Info("Login request parameters: ", &req)
	return
}

type CveIssueWhiteListController struct {
	beego.Controller
}

func (c *CveIssueWhiteListController) RetData(resp map[string]interface{}) {
	c.Data["json"] = resp
	c.ServeJSON()
}

// @Title Get issuewhitelist
// @Description get packages
// @Param	status	int	true (0,1,2)
// @Success 200 {object} models.IssueRepoWhitelist
// @Failure 403 :status is err
// @router / [get]
func (u *CveIssueWhiteListController) Get() {
	req := u.Ctx.Request
	addr := req.RemoteAddr
	logs.Info("Method: ", req.Method, "Client request ip address: ", addr,
		", Header: ", req.Header, ", body: ", req.Body)
	resp := make(map[string]interface{})
	var ird []IssueRepoWhiteData
	resp["errno"] = errcode.RecodeUnknowErr
	resp["errmsg"] = errcode.RecodeText(errcode.RecodeUnknowErr)
	resp["body"] = []IssueRepoWhiteData{}
	//defer u.RetData(resp)
	status, err := u.GetInt8("status", 0)
	if err != nil {
		logs.Error("status, err: ", err, ", status: ", status)
		resp["errno"] = errcode.RecodeParamErr
		resp["errmsg"] = errcode.RecodeText(errcode.RecodeParamErr)
		u.RetData(resp)
		return
	}
	issueWhiteData, issueErr := models.QueryIssueWhitelist(status)
	if issueErr == nil && len(issueWhiteData) > 0 {
		for _, issues := range issueWhiteData {
			var irda IssueRepoWhiteData
			irda.CreateTime = issues.CreateTime
			irda.Status = issues.Status
			irda.Version = issues.Version
			irda.PackageName = issues.PackageName
			irda.UpdateTime = issues.UpdateTime
			irda.RepoId = issues.RepoId
			irda.DeleteTime = issues.DeleteTime
			irda.Branchs = issues.Branchs
			ird = append(ird, irda)
		}
		resp["body"] = ird
		resp["errno"] = errcode.RecodeOk
		resp["errmsg"] = errcode.RecodeText(errcode.RecodeOk)
		u.RetData(resp)
	} else {
		resp["errno"] = errcode.RecodeNodata
		resp["errmsg"] = errcode.RecodeText(errcode.RecodeNodata)
		u.RetData(resp)
		return
	}
}
